package com.example.javabasic.thread.classtwo.workthread.demo2.future;

import com.example.javabasic.thread.classtwo.workthread.demo2.pool.TaskThreadPool;

import java.util.concurrent.Callable;

/**
 * @description future 工作类
 * @Author Lin FuYuan
 * @date 2021/2/25
 */
public class FutureService {

    public <T> TaskFuture<T> submit(Callable<T> callable){
        TaskFuture taskFuture = new TaskFuture();

        TaskThreadPool.getExecutor().submit(()->{
            T call = null;
            try {
                call = callable.call();
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("任务运行异常");
            }
            taskFuture.done(call);
        });
        return taskFuture;

    }
}
